package com.ql.controller.keshiControllrer;

import com.ql.pojo.keshi.FangShe;
import com.ql.pojo.keshi.Gonneng;
import com.ql.pojo.keshi.JanYan;
import com.ql.service.keshi.FangsheService;
import com.ql.service.keshi.GonnengService;
import com.ql.service.keshi.JianyanService;
import com.ql.utils.Constrants;
import com.ql.utils.PageHelper;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/keshi/")
public class KeshiController {

    @Autowired
    private FangsheService fangsheService;

    @Autowired
    private JianyanService jianyanService;

    @Autowired
    private GonnengService gonnengService;

    @RequestMapping("add")//进入放射展示
    public String  addInfo(@RequestParam(value = "name", required=false)String queryname,
                           Model model,
                           PageHelper ph){
        List<FangShe> fangSheList = fangsheService.getAllFangShe(ph,queryname);
        model.addAttribute("fangSheList",fangSheList);
        model.addAttribute("currentPageNo",ph.getCurrentPageNo());
        model.addAttribute("totalCount",ph.getTotalCount());
        model.addAttribute("totalPageCount",ph.getTotalPageCount());
        //把查询条件带到页面
        model.addAttribute("name",queryname);
            return "fangsheInfo";
    }


    @RequestMapping("fangsheInfo")
    public String  aredirectInfo(){

        return "fangsheInfo";
    }

//    @RequestMapping(value = "/upload",method = RequestMethod.POST)
//    @ResponseBody
//    public String uploadFile(){
//
//    }
    @RequestMapping("downadd")
    public String addpic(){

        return "download_addpic";
    }

    @RequestMapping("upload")
    @ResponseBody
    public String adduploadpic(){


        return "";
    }
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String save(FangShe fangShe, HttpSession session,
                       @RequestParam("attachs") MultipartFile mutitFile, Model model){
        String retCode  = "F";
        String retMsg = "";
        /**
         * 1.在statics目录下新建上传目录：upload文件夹；
         * 2.建立路径的映射；
         * 3.把相对存入数据库；
         */
        //取出文件名
        String fileName = mutitFile.getOriginalFilename();
        fangShe.setfPath(Constrants.UPLOAD_PATH+fileName);
        //取后缀名
        //String suffixName = mutitFile.getName();
        String suffixName = FilenameUtils.getExtension(fileName);
        if("jpg".equals(suffixName) || "png".equals(suffixName)){
            File file = new File(Constrants.UPLOAD_DISK_PATH_CY,fileName);//完整上传文件路径=磁盘文件夹+

            try {
                if(!file.exists()) {
                    file.createNewFile();
                }
                //umbnails.of(mutitFile.getInputStream()).scale(0.1f).outputQuality(0.1f).toFile(file);


                //写入到磁盘
               mutitFile.transferTo(file);
            } catch (IOException e) {
                //e.printStackTrace();
                System.out.println("上传失败，原因："+e.getMessage()+"hh");
                //retCode = "F";
                retMsg = "上传失败，请重新上传！";
            }
        }else{
            //retCode = "F";
            retMsg = "上传的文件格式只能是.jpg或者.png";
        }
      /*  FangShe loginFangShe = (FangShe) session.getAttribute(Constrants.FangShe_SESSION);
        fangShe.setCreatedBy(loginFangShe.getId());*/


        //3步骤，如无任何异常，返回成功
        retCode = "T";
        retMsg = "上传成功！";

       /* fangShe.setfTime(fangShe.getfTime());*/

     /*   Date date = DateF
        model.addAttribute("retCode",retCode);
        model.addAttribute("retMsg",retMsg);
        FangShe fangShe1 = new FangShe();
        fangShe1.setfCondition(fangShe.getfCondition());
        fangShe1.setfTime();*/
        //调用service方法进行保存
        if(fangsheService.addNewFangshe(fangShe)){
            return "picAddSuccess";
        }else{
            return "picAddFailed";

        }
    }
    /*
    删除
     */
    @RequestMapping("del/{fId}")
    @ResponseBody
    public String del(@PathVariable String fId, Model model){
        int fID = Integer.parseInt(fId);
        if(fangsheService.removeFangShe(fID)){
            String msg = "{\"retMsg\":true}";
            return msg;
        }else{
            String msg = "{\"retMsg\":false}";
            return msg;
        }
    }

    /*
        检验科代码
     */
    @RequestMapping("jianyan")//进入展示
    public String  addJianyanInfo(@RequestParam(value = "name", required=false)String queryname,
                           Model model,
                           PageHelper ph){
        List<JanYan> janYanList = jianyanService.getAllJanYan(ph,queryname);
        model.addAttribute("janYanList",janYanList);
        model.addAttribute("currentPageNo",ph.getCurrentPageNo());
        model.addAttribute("totalCount",ph.getTotalCount());
        model.addAttribute("totalPageCount",ph.getTotalPageCount());
        //把查询条件带到页面
        model.addAttribute("name",queryname);
        return "jianyanInfo";
    }
    @RequestMapping(value = "jysave",method = RequestMethod.POST)
    public String save(JanYan janYan, HttpSession session,
                        Model model){
        String retCode  = "F";
        String retMsg = "";

        //调用service方法进行保存
        if(jianyanService.addNewJanYan(janYan)){
            return "picAddSuccess";
        }else{
            return "picAddFailed";

        }
    }
    /*
 删除
  */
    @RequestMapping("jydel/{jId}")
    @ResponseBody
        public String jydel(@PathVariable String jId, Model model){
        int jID = Integer.parseInt(jId);
        if(jianyanService.removeJanYan(jID)){
            String msg = "{\"retMsg\":true}";
            return msg;
        }else{
            String msg = "{\"retMsg\":false}";
            return msg;
        }
    }
    /*
        功能科
     */
    @RequestMapping("gonneng")//进入展示
    public String  addGonnengInfo(@RequestParam(value = "name", required=false)String queryname,
                                  Model model,
                                  PageHelper ph){
        List<Gonneng> gonnengList = gonnengService.getAllGonneng(ph,queryname);
        model.addAttribute("gonnengList",gonnengList);
        model.addAttribute("currentPageNo",ph.getCurrentPageNo());
        model.addAttribute("totalCount",ph.getTotalCount());
        model.addAttribute("totalPageCount",ph.getTotalPageCount());
        //把查询条件带到页面
        model.addAttribute("name",queryname);
        return "gonnengInfo";
    }
    @RequestMapping(value = "gnsave",method = RequestMethod.POST)
    public String saveGon(Gonneng gonneng, HttpSession session,
                       Model model){
        String retCode  = "F";
        String retMsg = "";

        //调用service方法进行保存
        if(gonnengService.addNewGonneng(gonneng)){
            return "picAddSuccess";
        }else{
            return "picAddFailed";

        }
    }
}
